<?php

class Access {

    public $CI;
    public $acl;

    public function __construct() {
        $this->CI = & get_instance();
        ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . APPPATH . 'libraries');
        log_message('debug', "Zend Class Initialized");
        require_once 'Zend/Acl.php';
        require_once 'Zend/Acl/Role.php';
        require_once 'Zend/Acl/Resource.php';
        $this->acl = new Zend_Acl;
    }

    public function start() {

        $this->setRoles();
        $this->setResources();
        $this->setAccess();
        
        $controller = $this->CI->router->fetch_class();
        $action = $this->CI->router->fetch_method();
        $level = $this->CI->session->userdata('ses_level');
        $role = "admin";
        switch ($level) {
            case '1':
                $role = 'member';
                break;
            case '2':
                $role = 'admin';
                break;
        }
        if (!$this->acl->isAllowed($role,$controller, $action)) {
            redirect(base_url() . 'admin/main');
        }
    }

    public function setRoles() {
        $this->acl->addRole(new Zend_Acl_Role('admin'))
                ->addRole(new Zend_Acl_Role('guest'));
                
    }

    public function setResources() {
        $this->acl
                ->add(new Zend_Acl_Resource('main'))
                ->add(new Zend_Acl_Resource('dm_san_pham'))
                ->add(new Zend_Acl_Resource('hoa_don'))
                ->add(new Zend_Acl_Resource('khach_hang'))
                ->add(new Zend_Acl_Resource('san_pham'));
                
    }

    public function setAccess() {
        $this->acl
                ->allow('guest', array("main","san_pham"))
                ->allow('admin',NULL);
    }

}

?>